<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class Advertising extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'ad:put';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '广告投放';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $this->putAd(1,8);
        $this->putAd(2,1);
        $this->putAd(3,3);
        $this->putAd(4,3);
        $this->putAd(5,1);

        return Command::SUCCESS;
    }



    // 轮播图广告投放
    public function putAd($ad_category_id,$total)
    {
        $count =  DB::table('ads')
            ->where('ad_category_id',$ad_category_id)
            ->where('status',3)
            ->count('id');

        if ($count<$total){
            $limit = $total-$count;
            $ads = DB::table('ads')
                ->select(['id','days'])
                ->where('ad_category_id',1)
                ->where('status',2)
                ->limit($limit)
                ->get();

            foreach ($ads as $ad){
                $put_time  = date('Y-m-d H:i:s');
                $end_time = date('Y-m-d H:i:s',strtotime('+'.$ad->days.' day'));
                DB::table('ads')
                    ->where('id',$ad->id)
                    ->update([
                        'status' => 3,
                        'put_time' => $put_time,
                        'end_time' => $end_time
                    ]);
            }

        }


        return Command::SUCCESS;
    }
}
